const express = require('express');
const multer = require('multer');
const fs = require('fs');
const FormData = require('form-data');
const { filesUpload } = require('../modules/file');
const router = express.Router();
const OSS = require('../sdk/ali-oss');
const { qiniuKodoPutFile } = require('../sdk/qiniu-kodo');



// 设置存储配置
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/')  // 保存的路径，备注：需要自己创建
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now() + '.' + file.originalname.split('.').pop())
  }
});

const upload = multer({ storage: storage });

// 本地图片上传到coze
router.post('/upload', upload.single('file'), async (req, res) => {
  try {
    console.log('req.file', req.file)
    if (!req.file) {
      return res.status(400).send({
        code: 500,
        msg: '请上传附件',
        success: false
      });
    }
    // 创建一个FormData实例
    const formData = new FormData();
    // 添加文件到FormData对象
    formData.append('file', fs.createReadStream(req.file.path), {
      filename: req.file.originalname,
      contentType: req.file.mimetype,
      knownLength: req.file.size
    });
    const fileRes = await filesUpload(formData)
		res.json({
			success: true,
			code: 200,
			data: fileRes.data,
			errMsg: '成功'
		})
	} catch (error) {
		res.status(500).send()
	}
});
// oss - 图片上传到阿里云oss
// oss - 配置multer存储
const storage_oss = multer.memoryStorage(); // 使用内存存储，因为我们将直接上传到

const upload_oss = multer({ storage: storage_oss });
router.post('/upload/oss', upload_oss.single('file'), async (req, res) => {
  try {
    console.log('req.file', req.file)
    const file = req.file;
    if (!file) {
      return res.status(400).send({
        code: 500,
        msg: '请上传附件',
        success: false
      });
    }
    // 上传文件
    // 将文件流转换为Buffer
    const buffer = Buffer.from(file.buffer);

    // OSS文件名
    const fileName = `wechat-xcx/${Date.now()}-${file.originalname}`;
    console.log('222', fileName, buffer)
    // 上传到OSS
    const result = await OSS.put(fileName, buffer);
    console.log('qiniuputFile result', result)
    // 返回OSS文件URL
    res.json({
      code: 200,
      url: result.url,
      ossResult: result,
      success: true
    });
	} catch (error) {
		res.status(500).send()
	}
});


// oss - 配置multer存储
const storage_oss_kodo = multer.memoryStorage(); // 使用内存存储，因为我们将直接上传到

const upload_oss_kodo = multer({ storage: storage_oss_kodo });
// oss - 图片上传到七牛云
router.post('/upload/oss/kodo', upload_oss_kodo.single('file'), async (req, res) => {
  try {
    console.log('req.file', req.file)
    if (!req.file) {
      return res.status(400).send({
        code: 500,
        msg: '请上传附件',
        success: false
      });
    }
    // 上传文件
    const result = await qiniuKodoPutFile(req.file.buffer, req.file.originalname)
    console.log('qiniuputFile result', result)
    // 返回OSS文件URL
    res.json({
      code: 200,
      url: result.url,
      ossResult: result,
      success: true
    });
	} catch (error) {
		res.status(500).send()
	}
});

module.exports = router;